package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysBook;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 图书管理操作处理
 *
 * @author fly
 */
@RestController
@RequestMapping("/system/book")
public class SysBookController extends BaseController {
    @Autowired
    private ISysBookService bookService;
    /**
     * 获取图书列表
     */
    @PreAuthorize("@ss.hasPermi('system:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBook book)
    {
        startPage();
        List<SysBook> list = bookService.selectBookList(book);
        return getDataTable(list);
    }
    /**
     * 新增图书
     */
    @PreAuthorize("@ss.hasPermi('system:book:add')")
    @Log(title = "图书管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysBook book)
    {
        if (!bookService.checkBookNameUnique(book))
        {
            return error("新增图书'" + book.getBookName() + "'失败，图书名称已存在");
        }
        else if (!bookService.checkBookCodeUnique(book))
        {
            return error("新增图书'" + book.getBookName() + "'失败，图书编码已存在");
        }
        book.setCreateBy(getUsername());
        return toAjax(bookService.insertBook(book));
    }


    /**
     * 删除图书
     */
    @PreAuthorize("@ss.hasPermi('system:book:remove')")
    @Log(title = "图书管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bookIds}")
    public AjaxResult remove(@PathVariable Long[] bookIds)
    {

        return toAjax(bookService.deleteBookByIds(bookIds));
    }

    /**
     * 根据图书编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:book:query')")
    @GetMapping(value = "/{bookId}")
    public AjaxResult getInfo(@PathVariable Long bookId)
    {
        
        return success(bookService.selectBookById(bookId));
    }


    /**
     * 修改图书
     */
    @PreAuthorize("@ss.hasPermi('system:book:edit')")
    @Log(title = "图书管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysBook book)
    {
        if (!bookService.checkBookNameUnique(book))
        {
            return error("修改图书'" + book.getBookName() + "'失败，图书名称已存在");
        }
        else if (!bookService.checkBookCodeUnique(book))
        {
            return error("修改图书'" + book.getBookName() + "'失败，图书编码已存在");
        }
        book.setUpdateBy(getUsername());
        return toAjax(bookService.updateBook(book));
    }
    /**
     * 图书导出
     */
    @Log(title = "图书管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:book:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBook book)
    {
        List<SysBook> list = bookService.selectBookList(book);
        ExcelUtil<SysBook> util = new ExcelUtil<SysBook>(SysBook.class);
        util.exportExcel(response, list, "图书数据");
    }
    /**
     * 下载图书模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysBook> util = new ExcelUtil<SysBook>(SysBook.class);
        util.importTemplateExcel(response, "图书数据");
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:book:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysBook> util = new ExcelUtil<SysBook>(SysBook.class);
        List<SysBook> bookList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = bookService.importBook(bookList, updateSupport, operName);
        return success(message);
    }
}
